package com.example.esaclient;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;

import com.google.gson.Gson;



public class GetDataUser extends AsyncTask<Object, Integer, String> {
 

	
	private String URL;
	private  static final String METHOD_NAME = "/GetDataUser?";	
	TimelineActivity caller;
	
	public GetDataUser(TimelineActivity callerActivity){
		caller=callerActivity;
	}
	
	@Override
	protected String doInBackground(Object... params) {
		
		try{
			 
			 GlobalSettings gs = new GlobalSettings(caller);
			 URL = "http://"+gs.getPrefString(GlobalSettings.PREF_ESA_URL)+"/ServiceRest.svc";			 
			 String url =URL+METHOD_NAME+"id="+params[0];
					  
			 HttpClient client = new DefaultHttpClient();
		        HttpGet httpGet = new HttpGet(url);
		        try {
		            HttpResponse response = client.execute(httpGet);
		            HttpEntity entity = response.getEntity();
		             
		            if(entity != null) {
		                InputStream stream = entity.getContent();
		                String responseString = GlobalSettings.convertStreamToString(stream);		                
		                
		                Wrapper w = new Gson().fromJson(responseString, Wrapper.class);
		                   return w.GetDataUserResult[1];
		                   
		            }
		
		  } catch (ClientProtocolException e) {
	            e.printStackTrace();	            
	        } 
		        catch (IOException e) {
	            e.printStackTrace();
	           
	        }
	         
		}
	     catch (final Exception e) {
	         e.printStackTrace();   
	       
	     }
			return "";
		}
	
	
	   public class Wrapper {
	    	 public String[] GetDataUserResult;
	     }

	     protected void onPostExecute(String result) {
	    	 caller.GetDataUserResult(result);
	     }

	
	
}
